/*
 * app_filetransport_def.h
 *
 *  Created on: 2023年8月24日
 *      Author: HXF
 */

#ifndef DS_APP_INS_APP_FILETRANSPORT_DEF_H_
#define DS_APP_INS_APP_FILETRANSPORT_DEF_H_
#include "device.h"
#include "btl_errorcode.h"
#define FILE_TRANSPORT_PAYLOAD_MAX_LEN 236 //报文长度 - 报文headr15 - tail4
#define FILE_TRANSPORT_RETRY_COUNT (3)

#define HISTORY_LINK_HAVE_MAX_BLOCK (2)
#define HISTORY_LINK_HAVE_MAX_PAGE_NUM (16) //有测到总的文件数缓存到60时，堆用完：减少历史队列里能存放的上限
#define EACH_BLOCK_HAVE_MAX_PAGE_NUM (32)
#define EACH_BLOCK_MAX_ERR_COUNT (32)

#define OTA_WAIT_REQUEST_TIMEOUT  (229376UL)  //7s
#define OTA_WAIT_CONFIRM_TIMEOUT  (229376UL)  //7s

#define OTA_HANDSHAKE_TIMEOUT  (131072UL)//229376UL //(OTA_WAIT_REQUEST_TIMEOUT + OTA_WAIT_CONFIRM_TIMEOUT)  //14s

#define RFM_LINEUP_INTERVAL  (983UL)  //30ms
#define OTA_WAIT_ACK_INTERVAL  (3277UL)  //100ms
#define RFM_WAIT_FILE_TIMEOUT  (655360UL)  //20s
#define RFM_DELAY_REBOOT_TIMEOUT (65536UL)  //2s

#define HUB_BROADCAST_INTERVAL  (983UL)  //30ms
#define HUB_BROADCAST_ALLOWANCE  (3277UL)  //100ms

#define OTA_ERR_HANDSHAKE_CONN   (0x01)
#define OTA_ERR_TOO_MANY_BLOCK   (0x02)
#define OTA_ERR_TOO_MANY_COUNT   (0x03)
#define OTA_ERR_CHECK_CONN       (0x04)
#define OTA_ERR_SHA256           (0x05)
#define OTA_ERR_ABORT            (0x06)
#define OTA_ERR_NOT_SUPPORT      (0x07)
#define OTA_ERR_NO_FILE          (0x08)
#define OTA_ERR_NOERR            (0xff)

#ifdef HUB_DEVICE
#define FILE_TEST  (0)
#else
#define FILE_FLASH_ENABLE  (1)
#endif

typedef enum{
  FTYPE_UNKNOWN = 0x01,
  FTYPE_PICTURE,
  FTYPE_OTA,
  FTYPE_CONFIG,
  FTYPE_FLASH,
}FILE_TYPE_E;

typedef enum{
  FILE_EVENT_START,
  FILE_EVENT_ABORT,
  FILE_EVENT_FINISH,
  FILE_EVENT_WAIT_FINISH,
  FILE_EVENT_WAIT_TIMEOUT,
  FILE_EVENT_TX_NOTIFY,
  FILE_EVENT_TX_FILE,
  FILE_EVENT_TX_CHECK,
  FILE_EVENT_RCV_FILE,
  FILE_EVENT_RCV_CHECK,
}FILE_EVENT_E;

typedef enum{
  UNKNOWN_ROLE,
  PRODUCTION_ROLE,
  CONSUMPTION_ROLE,
}FILE_TRANSPORT_ROLE_E;
#if(0)
typedef enum{
  HANDSHAKE_IDLE,
  HANDSHAKE_MODE,
  HANDSHAKE_FRIST = HANDSHAKE_MODE,
  HANDSHAKE_SECOND,
}HANDSHAKE_MODE_E;
#endif
typedef enum{
  NO_TRANSPORT,
  PRODUCTION_NOTIFY,
  PRODUCTION_HANDSHAKE,
  CONSUMPTION_REQUEST,
  CONSUMPTION_CONFIRM,
  PRODUCTION_TRANSPORT_FILE,
  PRODUCTION_TRANSPORT_CHECK,
  CONSUMPTION_RECEVE,
  PRODUCTION_ABORT_TRANSPORT,
  CONSUMPTION_ABORT_TRANSPORT,
  PRODUCTION_FINISH_TRANSPORT,
  CONSUMPTION_FINISH_TRANSPORT,
}FILE_TRANSPORT_E;

typedef enum{
  FILE_LINK_1,
#ifdef HUB_DEVICE
  FILE_LINK_2,
  FILE_LINK_HISTORY,
#endif
}FILE_LINK_NUM_E;

typedef enum{
  OTA_MODE_NO_OTA = 0,
  OTA_MODE_OTA_ING,
}OTA_MODE_E;

typedef enum{
  FILE_VERIFY_UNKNOWN = 0,
  FILE_VERIFY_SHA256,
}FILE_VERIFY_MODE_E;

//UpdateMe   hub -> rfm
typedef struct{
  unsigned char bretry;
  unsigned char btype;
  unsigned char len;
  unsigned char ctrl;
  unsigned char id[OTA_ID_BUF_MAX]; //单次Ota最多128个设备
}production_broadcast_notify_payload_t;


//cmd 0x04 文件传输请求    rfm -> hub
typedef struct{
  unsigned char len;
  unsigned char ftype;
  unsigned char info[OTA_REQUEST_KEYVALUE_INFO_LENGTH]; //devicetype + modetype + hardtype + mac[8] + rfver[3] + sver[3]
}consumption_request_payload_t;

//cmd 0x06 节点文件传输接受    hub -> rfm
typedef struct{
  unsigned char key;
  unsigned char len;
  unsigned char ctrl;
  unsigned char ftype;
  unsigned char file_size[FILE_SIZE_LENGTH];
  unsigned char page_size;
}production_consult_payload_t;

//cmd 0x04 文件传输确认    rfm -> hub
typedef struct{
  unsigned char len;
  unsigned char ftype;
  unsigned char info[OTA_CONFIRM_KEYVALUE_INFO_LENGTH]; //devicetype + modetype + hardtype + mac[8] + rfver[3] + sver[3]
}consumption_confrim_payload_t;

//cmd 0x4B 文件校验请求    hub -> rfm
typedef struct{
  unsigned char system_id[3];
  unsigned char bretry;
  unsigned char btype;
  unsigned char len;
  unsigned char ctrl;
  unsigned char id[OTA_ID_BUF_MAX]; //单次Ota最多128个设备
}production_check_request_payload_t;

//cmd 0x04 文件校验结果    rfm -> hub
typedef struct{
  unsigned char block_1_info[5];
  unsigned char block_2_info[5];
  unsigned char block_3_info[5];
  unsigned char block_4_info[5];
  unsigned char block_5_info[5];
}block_check_result;

typedef struct{
  unsigned char block_id;
  unsigned char result;
}file_check_result;

typedef struct{
  unsigned char len;
  union{
    block_check_result  b_c;
    file_check_result f_c;
  };
}consumption_check_response_payload_t;

//cmd 0x4A 文件分块传输    hub -> rfm
typedef struct{
  unsigned char data_len; //辅助
  unsigned char block_id; //辅助
  unsigned char page_id[2];
  unsigned char data[228]; //含CRC32 224+4
}cache_page_file_t;

typedef struct{
  union{
    production_broadcast_notify_payload_t       production_broadcast_notify_payload;
    consumption_request_payload_t               consumption_request_payload;
    production_consult_payload_t                production_consult_payload;
    consumption_confrim_payload_t               consumption_confrim_payload;
    production_check_request_payload_t          production_check_request_payload;
    consumption_check_response_payload_t        consumption_check_response_payload;
    unsigned char                               data[FILE_TRANSPORT_PAYLOAD_MAX_LEN];
  }data;
}filetransport_msg_t;

typedef struct{
  unsigned char block_id;
  unsigned char err_count;
  unsigned char page_mask[4];
}block_flag_t;

typedef struct{
  unsigned char rfmid;
  unsigned char enable_ota;    //0 取消资格， 1允许升级
  unsigned char is_request;    //0 没收到，1收到
  unsigned char is_confirm;    //0 没收到，1收到
  unsigned char is_check;     //0 没收到，1收到
  unsigned char is_finish;    //
  block_flag_t block[5];     //
}ota_rfm_info_t;

extern bool file_log_enable;

#endif /* DS_APP_INS_APP_FILETRANSPORT_DEF_H_ */
